﻿#include "MLObj.h"
#include "Drawer.h"

struct _CaptionFrame {
	size_t start_frame_count;
	size_t end_frame_count;
	std::string content;
};

class Caption :public MLObj {
public:
	Caption(
		int frame_size_x,
		int frame_size_y,
		double frame_rate,
		double begin_secs,
		double end_secs,
		size_t total_frames,
		int loc_x,
		int loc_y,
		std::string caption_path,
		uint32_t font_size,
		std::string font_path,
		int32_t font_index,
		Color color,
		uint8_t alpha,
		std::string anchor_type,
		int anchor_x,
		int anchor_y,
		bool horizontal,
		float line_spacing,
		std::string alignment_type,
		FT_Library font_lib
	);
	void get_frame(size_t frame_index, MLFrame& frame);

private:
	GLuint _mix_glsl;
	GLuint _text_glsl;
	std::queue<_CaptionFrame> _text_frames;
	Text _text;
	size_t _start_frame_count;
	size_t _end_frame_count;
	GLuint _fbo;
	GLuint _texture;
	bool _buffered = false;

	static Anchor _get_anchor_type(std::string anchor_type);
	static TextAlignment _get_alignment_type(std::string type);
	static Text _get_text_obj(
		uint32_t font_size,
		std::string font_path,
		int32_t font_index,
		Color color,
		uint8_t alpha,
		int frame_size_x,
		int frame_size_y,
		int loc_x,
		int loc_y,
		Anchor anchor,
		int anchor_x,
		int anchor_y,
		bool horizontal,
		float line_spacing,
		TextAlignment alignment,
		GLuint text_glsl,
		FT_Library font_lib
	);
	static std::queue<_CaptionFrame> _read_lyrics(std::string caption_path, double frame_rate);
	static _CaptionFrame _read_lyric_line(std::string line, double frame_rate);
	static std::queue<_CaptionFrame> _connect_lyrics(std::queue<_CaptionFrame>& frames);
};